<?php

/*
 * 合作店铺管理
 * 2016.10.19
 */

namespace Qwadmin\Controller;


class PartnerStoreController extends ComController{

     private $shop_sites_id=''; // 默认商家Id
     
    public function index()
    {
        $sites_id = I('shop_sites_id');
        if($sites_id){
            $this->shop_sites_id = $sites_id;
        }
       
        if($this->shop_sites_id){
            $where['shop_id'] = $sites_id;
        }
        
        $p = isset($_GET['p']) ? intval($_GET['p']) : '1';
        $m = M('partner_store');
        $pagesize = 10; #每页数量
        $offset = $pagesize * ($p - 1); //计算记录偏移量
        $count = $m->where($where)->count();
        $list = $m
                ->join('as p LEFT JOIN zt_customer as c ON c.id = p.shop_id')
                ->join('LEFT JOIN zt_partner as pn ON pn.id = p.bind_partner')
                ->where($where)->limit($offset . ',' . $pagesize)
                ->field('p.*,c.shop_name,pn.name as bind_name')
                ->select();
		//		var_dump($list);
        $page = new \Think\Page($count, $pagesize);
        $page = $page->show();
        $this->assign('customer',M('customer')->select());
        $this->assign('option', M('partner')->select());
        $this->assign('list', $list);
        $this->assign('page', $page);
        $this->display();
    }
    
   //绑定中台店铺
    public function pull($id = 0)
    {
        $bind_partner = I('bind_partner');
        $m = M('partner_store');
        $id = I('id');
        $msg ['errno'] = 0;
        $msg ['error'] = "绑定成功";
      
        if($id && $bind_partner){
            
            $flag = $m->data(array('bind_partner'=>$bind_partner))->where(array('id'=>$id))->save();
        }
        if (!$flag) {
            $msg ['errno'] = 1;
            $msg ['error'] = "删除失败！";
        }

        $this->ajaxReturn($msg);
    }
}
